/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package example.hello;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.*;

/**
 *
 * @author doo
 */
public class InterfaceSQL {

    private Connection connexion = null;
    private Statement statement = null;
    private ResultSet resultSet = null;

    public enum TypeReq {

        select, update, truncate, insert
    };
    private final String DB = "monito";
    private final String DB_URL = "localhost";
    private final String DB_FULL_URL = "jdbc:mysql://" + DB_URL + "/" + DB;
    private final String USER = "monito";
    private final String PASS = "monito";

    public InterfaceSQL(TypeReq typeDeRequete, String requete) {
        try {
            connectDB();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Impossible de se connecter à la base!");
        }

        if (typeDeRequete != null) {
            try {
                sendReq(typeDeRequete, requete);
            } catch (SQLException e) {
                System.out.println("Erreur lors de l'execution");
                e.printStackTrace();
            }
        }
    }

    public InterfaceSQL() {
        try {
            connectDB();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnexion() {
        return connexion;
    }

    public void setConnexion(Connection connexion) {
        this.connexion = connexion;
    }

    public Statement getStatement() {
        return statement;
    }

    public void setStatement(Statement statement) {
        this.statement = statement;
    }

    public ResultSet getResultSet() {
        return resultSet;
    }

    public void setResultSet(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

    private void connectDB() throws SQLException {
        // On commence par charger le driver MySQL
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Impossible de se connecter à la base");
            e.printStackTrace();
        }
        //On se connecte à la bdd
//        System.out.println("Connexion à la base de données " + DB + " sur " + DB_URL);
//        connexion = DriverManager.getConnection(DB_FULL_URL, USER, PASS);
        connexion = DriverManager.getConnection("jdbc:mysql://localhost/monito?" + "user=monito&password=monito");
    }

    public void sendReq(TypeReq typeDeRequete, String requete) throws SQLException {
        // On crée un objet "statement" qui regroupe l'ensemble des infos à envoyer au SGBD pour une requête
        statement = connexion.createStatement();

        if (typeDeRequete == TypeReq.select) {
            // On exécute une fois la requête, elle ne sera pas affichée 
            resultSet = statement.executeQuery(requete);
        } else {
            statement.executeUpdate(requete);
        }
    }

    public void closeAll() {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (connexion != null) {
            try {
                connexion.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
